/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package df.servlet.guarantee;

import df.bean.db.conn.DBConn;
import df.bean.db.conn.DBConnection;
import df.bean.guarantee.ProcessGuaranteeBean;
import df.bean.guarantee.ProcessGuaranteeSummaryBean;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

/**
 *
 * @author nopphadon
 */
public class ProcessImportGuaranteeSrvl extends HttpServlet {
    private DBConnection conn = null;
    private DBConn cdb = null;
    private ProcessGuaranteeBean gpb;
    private ProcessGuaranteeSummaryBean gs;
    /** 
    * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
    * @param request servlet request
    * @param response servlet response
    */
    public void init() throws ServletException {
        super.init();

        conn = new DBConnection();
        conn.connectToLocal();
        cdb = new DBConn(conn.getConnection());
        try {
            cdb.setStatement();
            gpb = new ProcessGuaranteeBean(cdb);
            gs = new ProcessGuaranteeSummaryBean(cdb);
        } catch (Exception ex) {
            System.out.println("Exception test: "+ex);
        }
    }    

    @Override
    public void destroy(){
        super.destroy();
        conn.Close();
    }

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/xml; charset=UTF-8");
        
        PrintWriter out = response.getWriter();
        out.print("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
        
        request.setCharacterEncoding("UTF-8");
        try {
            String hospitalCode = request.getParameter("HOSPITAL_CODE");
            String year = request.getParameter("YYYY");
            String month = request.getParameter("MM");
            String process_type = request.getParameter("TYPE");
            boolean st = true;
            
            //if(process_type.equals("Prepare Guarantee Transaction")||process_type.equals("Set Guarantee Transaction")
            //||process_type.equals("Calculate Guarantee Transaction")){
            try {
                st = gpb.prepareProcess(month, year, hospitalCode, process_type);
                out.print("<RESULT><SUCCESS>" + st + "</SUCCESS></RESULT>");
            }catch (Exception  e) {
            	st = false;
                out.print("<RESULT><SUCCESS>" + st + "</SUCCESS></RESULT>");
                System.out.println("Prepare error : "+e+" / Process : "+month+year+hospitalCode+process_type);
            }finally { 
                out.close();
            }
            //}
            /*
            if(process_type.equals("Summary Guarantee Transaction")||process_type.equals("Summary Guarantee Tax")
                ||process_type.equals("Summary Guarantee Monthly")){
                System.out.println(process_type);
                try {
                    out.print("<RESULT><SUCCESS>" + gs.summaryProcess(month, year,hospitalCode, process_type) + "</SUCCESS></RESULT>");
                }catch (Exception  e) {
                    e.printStackTrace(out);
                }finally { 
                    out.close();
                }
            }
            
            if(process_type.equals("Delete Old Records")){
                System.out.println(process_type);
                try {
                    //out.print("<RESULT><SUCCESS>" + gr.processRollback(month, year, hospitalCode) + "</SUCCESS></RESULT>");
                }catch (Exception  e) {
                    e.printStackTrace(out);
                }finally { 
                    out.close();
                }
            }
            if(process_type.equals("Summary Guarantee Accu")){
                out.print("<RESULT><SUCCESS>true</SUCCESS></RESULT>");
                
                System.out.println(process_type);
                try {
                    out.print("<RESULT><SUCCESS>" + ga.summaryAbsorbAccu(month, year,hospitalCode) + "</SUCCESS></RESULT>");
                }catch (Exception  e) {
                    e.printStackTrace(out);
                }finally { 
                    out.close();
                }
                
            }
            

             TODO output your page here
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet ProcessImportGuarantee</title>");  
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet ProcessImportGuarantee at " + request.getContextPath () + "</h1>");
            out.println("</body>");
            out.println("</html>");
            */
        } finally { 
            out.close();
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
    * Handles the HTTP <code>GET</code> method.
    * @param request servlet request
    * @param response servlet response
    */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
    * Handles the HTTP <code>POST</code> method.
    * @param request servlet request
    * @param response servlet response
    */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
    * Returns a short description of the servlet.
    */
    public String getServletInfo() {
        return "Short description";
    }
    // </editor-fold>
}
